#! /bin/sh

#####################################
# PREPARE variables:
#
# NOTE that "app"/Contents specify slash. 
# this is not a problem even if app itself have slash at end.
# We will get something as  gg.app//Contents and this still works fine.
#
app=$1
app_vcomp="$app"/Contents/vcomponents

# YOU can change this to 0 to skip VREPORT installation
install_vreports=1


#####################################
# COPY files of vcomponents into APP.
#####################################

if [ ! -d "$app_vcomp" ] ; then
	mkdir "$app_vcomp"
fi

# VSHARED
if [ ! -e "$app_vcomp"/libvshared_fat_release.dylib ] ; then
	cp /usr/local/lib/vcomponents/libvshared_fat_release.dylib 	"$app_vcomp"
fi

# VKERNEL
if [ ! -e "$app_vcomp"/libvkernel_fat_release.dylib ] ; then
	cp /usr/local/lib/vcomponents/libvkernel_fat_release.dylib 	"$app_vcomp"
fi

# VCLIENT
if [ ! -e "$app_vcomp"/libvclient_fat_release.dylib ] ; then
	cp /usr/local/lib/vcomponents/libvclient_fat_release.dylib 	"$app_vcomp"
fi

# VREPORT
if [ ! -e "$app_vcomp"/libvreport_fat_release.dylib ] ; then
	cp /usr/local/lib/vcomponents/libvreport_fat_release.dylib 	"$app_vcomp"
fi


####################################
# Copy Folders:
####################################

# vresources
cp -R /usr/local/lib/vcomponents/vresources 					"$app_vcomp"


#####################################
# CHANGE executable_path for engine dylibs:
#####################################

	# change id of VSHARED
	install_name_tool -id "@executable_path/../vcomponents/libvshared_fat_release.dylib" "$app_vcomp"/libvshared_fat_release.dylib

	# change id of VKERNEL
	install_name_tool -id "@executable_path/../vcomponents/libvkernel_fat_release.dylib" "$app_vcomp"/libvkernel_fat_release.dylib

	install_name_tool -change "/usr/local/lib/vcomponents/libvshared_fat_release.dylib"   "@executable_path/../vcomponents/libvshared_fat_release.dylib" 	"$app_vcomp"/libvkernel_fat_release.dylib
	install_name_tool -change "/usr/local/lib/vcomponents/VSDK.framework/Versions/A/VSDK" "@executable_path/../vcomponents/VSDK.framework/Versions/A/VSDK" 	"$app_vcomp"/libvkernel_fat_release.dylib

	# change id of VCLIENT
	install_name_tool -id "@executable_path/../vcomponents/libvclient_fat_release.dylib" "$app_vcomp"/libvclient_fat_release.dylib
	install_name_tool -change "/usr/local/lib/vcomponents/libvshared_fat_release.dylib" "@executable_path/../vcomponents/libvshared_fat_release.dylib" "$app_vcomp"/libvclient_fat_release.dylib

	# change id of VREPORT
	install_name_tool -id "@executable_path/../vcomponents/libvreport_fat_release.dylib" "$app_vcomp"/libvreport_fat_release.dylib
	install_name_tool -change "/usr/local/lib/vcomponents/libvshared_fat_release.dylib" "@executable_path/../vcomponents/libvshared_fat_release.dylib" 	"$app_vcomp"/libvreport_fat_release.dylib
	install_name_tool -change "/usr/local/lib/vcomponents/libvkernel_fat_release.dylib" "@executable_path/../vcomponents/libvkernel_fat_release.dylib" 	"$app_vcomp"/libvreport_fat_release.dylib
	install_name_tool -change "/usr/local/lib/vcomponents/libvclient_fat_release.dylib" "@executable_path/../vcomponents/libvclient_fat_release.dylib" 	"$app_vcomp"/libvreport_fat_release.dylib

    
    
#####################################
# now we need to update yet APP/Contents/MacOS/Externals/v4rev.bundle/Contents/MacOS/v4rev
#####################################
app_v4rev="$app"/Contents/MacOS/Externals/v4rev.bundle/Contents/MacOS/v4rev

if [ -e "$app_v4rev" ] ; then
	install_name_tool -change "/usr/local/lib/vcomponents/libvshared_fat_release.dylib" 	"@executable_path/../vcomponents/libvshared_fat_release.dylib" 		"$app_v4rev"
	install_name_tool -change "/usr/local/lib/vcomponents/libvkernel_fat_release.dylib" 	"@executable_path/../vcomponents/libvkernel_fat_release.dylib" 		"$app_v4rev"
	install_name_tool -change "/usr/local/lib/vcomponents/libvclient_fat_release.dylib" 	"@executable_path/../vcomponents/libvclient_fat_release.dylib" 		"$app_v4rev"
	install_name_tool -change "/usr/local/lib/vcomponents/libvreport_fat_release.dylib" 	"@executable_path/../vcomponents/libvreport_fat_release.dylib" 		"$app_v4rev"
fi

#####################################
# now we need to update yet APP/Contents/MacOS/Externals/dbvalentina2/Contents/MacOS/dbvalentina2
#####################################
app_dbvalentina2="$app"/Contents/MacOS/Externals/database_drivers/dbvalentina2/Contents/MacOS/dbvalentina2

if [ -e "$app_dbvalentina2" ] ; then
	install_name_tool -change "/usr/local/lib/vcomponents/libvshared_fat_release.dylib" 	"@executable_path/../vcomponents/libvshared_fat_release.dylib" 		"$app_dbvalentina2"
	install_name_tool -change "/usr/local/lib/vcomponents/libvkernel_fat_release.dylib" 	"@executable_path/../vcomponents/libvkernel_fat_release.dylib" 		"$app_dbvalentina2"
	install_name_tool -change "/usr/local/lib/vcomponents/libvclient_fat_release.dylib" 	"@executable_path/../vcomponents/libvclient_fat_release.dylib" 		"$app_dbvalentina2"
	install_name_tool -change "/usr/local/lib/vcomponents/libvreport_fat_release.dylib" 	"@executable_path/../vcomponents/libvreport_fat_release.dylib" 		"$app_dbvalentina2"
fi

